var autoSave = {
	data:null,
	/*缓冲区名称*/
	name:'data',
	init:function(){
		/*使用userDatac存储，userData必须创建一个html标签，然后将behavior:url(‘#default#userData’)样式属性加上去*/
		autoSave.data=document.createElement('input');	
		autoSave.data.type='hidden';
		autoSave.data.style.display='none';
		autoSave.data.style.behavior="('#default#userData')";
		document.body.appendChild(autoSave.data);
		/*设置时间*/
		var expirs = new Date();
		expirs.setDate(expirs.getDate()+365);
		/*转换为国际标准时间*/
		autoSave.data.expirs = expirs.toUTCString();
		
	
	},
	setItem:function(key,value){
		this.init();
		autoSave.data.load(autoSave.name);
		autoSave.setAttribute(key,value);
		autoSave.save(autoSave.name);
	},
	getItem:function(key){
		this.init();
		autoSave.data.load(autoSave.name);
		return autoSave.getAttribute(key);
	},
	remove:function(key){
		this.init();
		autoSave.data.load(autoSave.name);
		autoSave.data.remove(key);
		autoSave.data.save(autoSave.name);
	
	},
	//是否支持localStorage
	isLocalStorage:function(){
		if(window.localStorage){
			return true;
		}else{
			return false;
		}
	}
}
/**
 * 获取值
 * */
function getData(){
	/*组装数据*/
	var data={
		title:'',
		textArer:'',
		labelData:'',
		typeData:''
	}
	data.title=document.getElementById('title').value;
	data.textArer=document.getElementById('textArer').value;
	data.labelData=document.getElementById('labelData').value;
	data.typeData=document.getElementById('typeData').value;
	return data;
}
/**
 * 设置值
 * */
function setData(data,flag){
	if(flag){
		if(data == "" || data=={}){
			document.getElementById('title').value="";
			document.getElementById('textArer').value="";
			document.getElementById('labelData').value="";
			document.getElementById('typeData').value="";
		}else{
			document.getElementById('title').value=data.title;
			document.getElementById('textArer').value=data.textArer;
			document.getElementById('labelData').value=data.labelData;
			document.getElementById('typeData').value=data.typeData;
		}
	}else{
		if(data == "" || data=={}){
			document.getElementById('title').value="";
			document.getElementById('textArer').value="";
			document.getElementById('labelData').value="";
			document.getElementById('typeData').value="";
		}else{
			document.getElementById('title').value=data.title;
			document.getElementById('textArer').value=data.textArer;
			document.getElementById('labelData').value=data.labelData;
			document.getElementById('typeData').value=data.typeData;
		}
		
	}
}
/*
 *json字符串转为json对象 
 * */
function jsonStringToJsonObj(data){
	return eval('(' + data + ')');
}
/*当页面离开时*/
window.onbeforeunload=function(){
	var data=getData();
	var jsonData=JSON.stringify(data);
	if(autoSave.isLocalStorage()){
		localStorage.setItem(autoSave.name,jsonData);
	}else{
		autoSave.setItem(autoSave.name,jsonData);
	}
};
//当页面加载时
window.onload = function(){
	if(autoSave.isLocalStorage()){
		var data=localStorage.getItem(autoSave.name);
		var JsonObj=jsonStringToJsonObj(data);
		setData(JsonObj,true);
	}else{
		var data=autoSave.getItem(autoSave.name);
		var JsonObj = jsonStringToJsonObj(data);
		setData(JsonObj,false);
	}
		
}

document.getElementById("save").onclick=function(){
	var data="";
	if(autoSave.isLocalStorage()){
		localStorage.removeItem(autoSave.name);
		setData(data,true);
	}else{
		autoSave.remove(autoSave.name);
		setData(data,false);
	}
	
}
